package emulator.hardware.bus;

import emulator.hardware.debug.BusWatchException;
import emulator.hardware.io.Via6522;
import emulator.hardware.memory.MemoryBlock;
import emulator.hardware.memory.UnmappedMemoryException;

public class FloppyBus implements Bus
{
	private MemoryBlock memory;
	private Via6522 via1;
	private Via6522 via2;

	public FloppyBus(MemoryBlock memory, Via6522 via1, Via6522 via2)
	{
		this.memory = memory;
		this.via1 = via1;
		this.via2 = via2;
	}

	@Override
	public void write(int address, int data) throws BusWatchException, UnmappedMemoryException
	{
		if (address < 0x0800) // RAM
			memory.write(address, data);
		else if (address >= 0xC000)
			; // NOP (ROM)
		else if (0x1800 <= address && address < 0x1810) // VIA1
			via1.write(address & 0x000F, data);
		else if (0x1C00 <= address && address < 0x1C10) // VIA2
			via2.write(address & 0x000F, data);
		// else do nothing
	}

	@Override
	public int read(int address) throws BusWatchException, UnmappedMemoryException
	{
		if (address < 0x0800 || address >= 0xC000) // RAM & ROM
			return memory.read(address);
		else if (0x1800 <= address && address < 0x1810) // VIA1
			return via1.read(address & 0x000F);
		else if (0x1C00 <= address && address < 0x1C10) // VIA2
			return via2.read(address & 0x000F);
		else
			return (address >> 8) & 0xff;
	}
}
